﻿using ArtMath.Attribute;
using ArtMath.Component.Point;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.Data;
using ArtMath.Resource;
using System;

namespace ArtMath.Component.Line
{
    /// <summary>
    /// 约束直线
    /// </summary>
    [Serializable]
    public class ConstrainedStraightLine : PointSlopeLineBase
    {
        PadStringX slope;
        public ConstrainedStraightLine(SketchPad pad, string name, string k, bool addToList = true) : base(pad, name, ToolTypeName.yszx, addToList)
        {
            InPoints = new PointBase[1];
            StrSlope = new PadStringX(Pad, k);
            Slope = StrSlope.Value;
        }
        public override double NewSlope
        {
            get
            {
                return StrSlope.Value;
            }
        }
        void Slope_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            PositionPoint.TempMoveLock = true;
            if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
            {
                ComponentChanged(this);
            }
            else
            {

            }
            Pad.MoveObject_DoMouseUp(this);
            PositionPoint.TempMoveLock = false;
        }
        void Slope_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            PositionPoint.TempMoveLock = true;
            if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
            {
                ComponentChanged(this);
            }
            else
            {
                Pad.ShowMessage(string.Format(Resources.Translate("FailModify"), OfTranslator("Slope")), MessageType.Error);
                StrSlope.SetExpression(e.OldExpression, false);
            }
            Pad.MoveObject_DoMouseUp(this);
            PositionPoint.TempMoveLock = false;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("Slope")]
        public PadStringX StrSlope
        {
            get { return slope; }
            set
            {
                if (slope is object)
                {
                    slope.ParametersValueChanging -= Slope_ParametersValueChanging;
                    slope.ParametersValueChanged -= Slope_ParametersValueChanged;
                    slope.ExpressionSetting -= Slope_ExpressionSetting;
                    slope.ExpressionSet -= Slope_ExpressionSet;
                }
                slope = value;
                if (slope is object)
                {
                    slope.ParametersValueChanging += Slope_ParametersValueChanging;
                    slope.ParametersValueChanged += Slope_ParametersValueChanged;
                    slope.ExpressionSetting += Slope_ExpressionSetting;
                    slope.ExpressionSet += Slope_ExpressionSet;
                }
            }
        }

        void Slope_ExpressionSetting(StringX sender)
        {
            PositionPoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            PositionPoint.TempMoveLock = false;
        }

        void Slope_ParametersValueChanging(DocStringX sender)
        {
            PositionPoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            PositionPoint.TempMoveLock = false;
        }

        [MultilingualCategory("Structure"), MultilingualDisplayName("AnchorPoint")]
        public PointBase PositionPoint
        {
            get { return InPoints[0]; }
            set { SetPoint(0, value); }
        }
        public override void ComponentBuilding(Base component)
        {
            if (PositionPoint == null)
            {
                pske.Location = SpareCoordinate;
                ske.Location = Pad.PxyToXY(pske.Location);
                ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{string.Format(Resources.Translate("Of"), Resources.JoinLocalizedString("AnchorPoint"), "Coordinate")}{Resources.ColonSeparate}{Pad.PxyToMath(pske.Location)}");
                base.ComponentBuilding(component);
            }
        }
        public void CreateComponents(PointBase pt)
        {
            if (PositionPoint == null)
            {
                PositionPoint = pt;
                SpareCoordinate_ = PositionPoint.Pxy;
                Pad.TrackObj = this;
                base.CreateComponents();
            }
        }
        public override void CreateComponents()
        {
            if (PositionPoint == null)
            {
                CreateComponents(Pad.CreatePoint(Resources.Translate("AnchorPoint"), SpareCoordinate_));
            }
        }
        public override object CopyAndPaste(SketchPad pad)
        {
            ConstrainedStraightLine cs = new ConstrainedStraightLine(pad, Utility.GetNextAvailableName(Name, pad.CheckObjectNameExist), StrSlope.DirectExpression, LayerItem != null);
            afterNewCopy(cs);
            return cs;
        }
    }
}
